package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    //新增菜品
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息
        this.save(dishDto);

        //为菜品口味设置和菜品关联的id
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));

        //保存菜品的口味
        dishFlavorService.saveBatch(flavors);

    }



    //修改菜品信息时回显数据
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //根据id查询里面的菜品信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        //把dish的数据封装到dishDto中
        BeanUtils.copyProperties(dish, dishDto);

        //查询dishFlavor里面的口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);

        dishDto.setFlavors(list);

        return dishDto;



    }


    //修改菜品信息
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表的菜品信息
        this.updateById(dishDto);

        //删除该菜品下原有的口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(wrapper);

        //添加修改后的菜品口味信息,并且还要为当前修改的菜品赋id的值
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor ->
                dishFlavor.setDishId(dishDto.getId())
        );

        dishFlavorService.saveBatch(flavors);

    }


    //删除菜品信息
    /*
    * 查询该菜品的售卖状态,如果正在售卖中,则不能删除(根据id查询,status = 1 > 0 则表示有菜品正在售卖,不能删除,),
    * 如果停售中,则直接删除,先删除菜品的基本信息(dish表),然后再删除菜品里面的口味(dish_flavor ,根据dishID进行删除)
    * */
    @Transactional
    @Override
    public void removes(List ids) {

        //查询是否是售卖状态,先删除该菜品的信息
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId, ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1);
        int count = this.count(dishLambdaQueryWrapper);

        if (count > 0) {
            throw new CustomException("该菜品正在售卖中,不能删除");
        }else{
            this.removeByIds(ids);
        }

        //删除菜品里面的口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);


    }


}